package com.erclab.internal.xpresso.webapp.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.erclab.internal.xpresso.forms.Form;
import com.erclab.internal.xpresso.webapp.WebObjectFactory;
import com.erclab.internal.xpresso.xml.ObjectToXml;

public class FormToXML extends FormConstructor {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException, SecurityException {
		
		try {
			WebObjectFactory.createAndAddForm(request, extractFormName(request.getServletPath()));
		} catch (SecurityException e) {
			// TODO log4j
			throw new SecurityException(e.getMessage());
		} catch (Exception e) {
			// TODO log4j
			throw new ServletException(e);
		}

		Form theForm = (Form)request.getAttribute("Form");
		PrintWriter out = response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		out.print(ObjectToXml.createFormXML(theForm));
		response.flushBuffer();
}
	
}
